#include<bits/stdc++.h>
// #define gc getchar
#define itn int
#define x first
#define y second
#define eb emplace_back
#define em emplace
#define pb push_back
#define db double
#define y1 yy1_yyds
using namespace std; typedef long long ll; typedef unsigned long long ull;
// https://www.luogu.com.cn/discuss/522581 About "const"
char buf[(1<<21)+5],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
ll read() {
	ll x = 0; short fh = 1; char ch = gc();
	while (!isdigit(ch)) {
		if (ch == '-') fh = -1;
		if (ch < 10) exit(0); 
		ch = gc();
	}
	while (isdigit(ch))
		x = x * 10 + (ch ^ 48), ch = gc();
	return fh * x;
}
#ifndef ONLINE_JUDGE
void debug() {cerr << "\n";}
template<typename Typ1> void debug(Typ1 arg) {cerr << arg << "\n";}
template<typename Typ1, typename ...Typ2> void debug(Typ1 arg, Typ2 ...args) {
	cerr << arg << " ", debug(args...);
}
#else
void debug() {}
template<typename Typ1> void debug(Typ1 arg) {}
template<typename Typ1, typename ...Typ2> void debug(Typ1 arg, Typ2 ...args) {}
#endif
void writeln(ll arg) {printf("%lld\n", arg);}
template<typename ...Typ2> void writeln(ll arg, Typ2 ...args) {
	printf("%lld ", arg), writeln(args...);
}
typedef pair <int, int> pii; typedef pair <ll, ll> pll;
const char Y_E_S[] = "YES", N__O[] = "NO";
// const char Y_E_S[] = "Yes", N__O[] = "No";
// #define infinite_testcase
// #define multiple_testcase
// #define output_Yes_No
bool memory_start;
bool isprime(int p) {
	for(int i = 2; i <= p / i; i++)
		if(p % i == 0) return 0;
	return 1;
}
const int DUST = 327, N = 1010, M = 2e6 + 10;
const int mod2 = 147744151;
int n, m, q, mod;
ll a[N][N];
namespace Sub0 {
	namespace segtree {
		// int T[N << 2];
		struct node2 {
			int T[N << 2];
		}T[N << 2];
		void pushup1(int _2, int u) {T[_2].T[u] = T[_2].T[u << 1] * 1ll * T[_2].T[u << 1 | 1] % mod;}
		void pushup2(int u) {
			for(int i = 1; i <= /*n*/m << 2; i++)
				// pushup1(u, i);
				T[u].T[i] = T[u << 1].T[i] * 1ll * T[u << 1 | 1].T[i] % mod;
		}
		void build1(int _2, int i, int u, int l, int r) {
			if(l == r) {
				T[_2].T[u] = a[/*_2*/i][l];
				return;
			}
			int mid = (l + r) >> 1;
			build1(_2, i, u << 1, l, mid), build1(_2, i, u << 1 | 1, mid + 1, r), pushup1(_2, u);
		}
		void build2(int u, int l, int r) {
			if(l == r) {
				build1(u, l, 1, 1, m);
				return;
			}
			int mid = (l + r) >> 1;
			build2(u << 1, l, mid), build2(u << 1 | 1, mid + 1, r), pushup2(u);
		}
		int query1(const int &_2, int u, const int &l, const int &r, int nl, int nr) {
			if(nl >= l && nr <= r) return T[_2].T[u];
			int nmid = (nl + nr) >> 1;
			int res = 1;
			if(nmid >= l) res = query1(_2, u << 1, l, r, nl, nmid);
			if(nmid < r) res = res * 1ll * query1(_2, u << 1 | 1, l, r, nmid + 1, nr) % mod;
			return res;
		}
		int x1, x2, y1, y2;
		int query2(int u, int nl, int nr) {
			if(nl >= x1 && nr <= x2) return query1(u, 1, y1, y2, 1, m);
			int nmid = (nl + nr) >> 1;
			int res = 1;
			if(nmid >= x1) res = query2(u << 1, nl, nmid);
			if(nmid < x2) res = res * 1ll * query2(u << 1 | 1, nmid + 1, nr) % mod;
			return res;
		}
	}
	using namespace segtree;
	void major() {
		build2(1, 1, n);
		int output = 0;
		for(int i = 1; i <= q; i++) {
			/*int */x1 = read(), y1 = read(), x2 = read(), y2 = read();
			int res = query2(1, 1, n);
			output = (output + (i ^ res)) % mod2;
		}
		writeln(output);
	}
}
namespace Sub1 {
	void major() {
		int output = 0;
		for(int i = 1; i <= q; i++) {
			int x1 = read(), y1 = read(), x2 = read(), y2 = read();
			int res = /*0*/1;
			for(int i0 = x1; i0 <= x2; i0++)
				for(int j0 = y1; j0 <= y2; j0++)
					res = (res /*+*/* a[i0][j0]) % mod;
			// debug(res);
			output = (output + (i ^ res)) % mod2;
		}
		writeln(output);
	}
}
namespace Sub3 {
	// const int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
	int primes[27], idx1 = 0; bool vis[114];
	void Euler() {
		for(int i = 2; i <= 100; i++) {
			if(!vis[i]) primes[idx1++] = i;
			for(int j = 0; primes[j] <= 100 / i; j++) {
				assert(j < idx1);
				vis[i * primes[j]] = /*0*/1;
				if(i % primes[j] == 0) break;
			}
		}
	}
	int cnt[N][N][25], qq[25];
	ll qp[N][25];
	void major() {
		Euler();
		// debug(idx1);
		// assert(idx1 == 25);
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= m; j++) {
				for(int k = 0; k < 25; k++) {
					while(a[i][j] % primes[k] == 0) a[i][j] /= primes[k], cnt[i][j][k]++;
					cnt[i][j][k] += cnt[i - 1][j][k] + cnt[i][j - 1][k] - cnt[i - 1][j - 1][k];
					qq[k] = max(qq[k], cnt[i][j][k]);
				}
			}
		for(int k = 0; k < 25; k++) {
			qp[0][k] = 1;
			for(int l = 1; l <= qq[k]; l++)
				qp[l][k] = qp[l - 1][k] * /*k*/primes[k] % mod;
		}
		int output = 0;
		for(int i = 1; i <= q; i++) {
			int x1 = read(), y1 = read(), x2 = read(), y2 = read();
			x1--, y1--;
			int res = 1;
			for(int k = 0; k < 25; k++) {
				int r = cnt[x1][y1][k] + cnt[x2][y2][k] - cnt[x1][y2][k] - cnt[x2][y1][k];
				// assert(r <= qq[k]);
				res = res * qp[r][k] % mod;
			}
			output = (output + (i ^ res)) % mod2;
		}
		writeln(output);
	}
}
namespace Sub5 {
	ll qp(ll a, ll b) {
		ll res = 1;
		for(; b; b >>= 1, a = a * a % mod) if(b & 1) res = res * a % mod;
		return res;
	}
	ll b[N][N];
	void major() {
	/**/for(int i = 0; i <= 1000; i++) a[i][0] = a[0][i] = /**/b[i][0] = b[0][i] = 1;
		for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) {
			if(i - 1) (a[i][j] *= a[i - 1][j]) %= mod;
			if(j - 1) (a[i][j] *= a[i][j - 1]) %= mod;
			if(i - 1 && j - 1) (a[i][j] *= /*qp(a[i - 1][j - 1], mod - 2)*/b[i - 1][j - 1]) %= mod;
			b[i][j] = qp(a[i][j], mod - 2);
		}
		int output = 0;
		for(int i = 1; i <= q; i++) {
			int x1 = read(), y1 = read(), x2 = read(), y2 = read();
			x1--, y1--;
			// int res = a[x1][y1] * a[x2][y2] % mod * qp(a[x1][y2], mod - 2) % mod * qp(a[x2][y1], mod - 2) % mod;
			int res = a[x1][y1] * a[x2][y2] % mod * b[x1][y2] % mod * b[x2][y1] % mod;
			output = (output + (i ^ res)) % mod2;
		}
		writeln(output);
	}
}
bool memory_end;
bool major(int Case = 1) {
	// debug((&memory_start - &memory_end) / 1048576., "MB");
	n = read(), m = read(), mod = read();
	for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) a[i][j] = read();
	q = read();
	if(max({n, m, q}) <= 200) return Sub1::major(), 0;
	if(mod <= 100) return Sub3::major(), 0;
	if(isprime(mod)) return Sub5::major(), 0;
	return Sub0::major(), 0;
	return Case ^= Case ^ Case;
}
void initial_function(int argc, char **argv) {
	**argv = argc; /* <- place_holder
	you won't give up no matter what happens, will you?
	code time: >> 10:29
	---
	签到只能拿 80pts 的菜狗。
	*/
	freopen("tsuki.in", "r", stdin);
	freopen("tsuki.out", "w", stdout);
}
signed main(int argc, char **argv) {
	initial_function(argc, argv);
	int Case = 1, Maxcase = 1;
	for (
#ifdef multiple_testcase
		  Maxcase = read()
#endif
				     	  ;
#ifndef infinite_testcase
							Case <= Maxcase
#endif
				     					   ; Case++)
#ifdef output_Yes_No
		puts(major(Case) ? Y_E_S : N__O);
#else
		major(Case);
#endif
	return DUST ^ 0x147;
}
